/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package input;

import editor.WorldQ;
import graphic.Graphic;
import java.util.LinkedList;
import java.util.Queue;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import klient.Klient;

/**
 *  
 * @author Marcin & Bartek
 * 
 * @description Controls input - keyboard and mouse
 */
enum iEvent{
    LMCLICK,
    RMCLICK,
    LMGEST,
    RMGEST,   
}

public final class Input {
    private Input(){}
    public static LinkedList<iEvent> q = new LinkedList<>();
    
    public static boolean LMBdown=false;
    public static boolean RMBdown=false;
    public static boolean LMBlock=false;
    public static boolean RMBlock=false;
    
    public static boolean reversed=false;
    
    public static int Mwhell=0;
    
    public static int x;
    public static int y;
    
    public static boolean LSgesture=false;
    public static boolean Lgesture=false;
    public static int lgsX,lgsY,lgeX,lgeY,lgdX,lgdY;
    
    public static boolean RSgesture=false;
    public static boolean Rgesture=false;
    public static int rgsX,rgsY,rgeX,rgeY,rgdX,rgdY;
    
    public static boolean LMBdown(){
        if(LMBdown){
            if(LMBlock) return false;
            else{
                LMBlock=true;
                return true;
            }
        }
        return false;
    }
    
    public static void init(){
        if(!Mouse.isCreated()){
            try{
                Mouse.create();
            }catch(LWJGLException e) {
                Klient.log("Nie można zainicjalizować myszki!");
            }
        }
        if(!Keyboard.isCreated()){
            try{
                Keyboard.create();
            }catch(LWJGLException e) {
                Klient.log("Nie można zainicjalizować klawiatury!");
            }
        }
    }
    
    public static boolean isKeyDown(int key){
        return Keyboard.isKeyDown(key);
    }
    
    public static int x(){
        return x;
    }
    public static int y(){
        return (reversed?Graphic.e.getHeight()-y:y);
    }
    
    public static void clear(){
        while(Keyboard.next()){}
        
    }
    
    public static boolean ifKeyPressed(int key){
        if(Input.isKeyDown(key)){
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                Input.clear();
                return true;
        }return false;
    }
    
    public static boolean mouseIn(int x1,int x2,int y1,int y2){
        if(reversed){
            y1=Graphic.e.getHeight()-y1;
            y2=Graphic.e.getHeight()-y2;
            int buf=y1;
            y1=y2;
            y2=buf;
        }
        return (x1<x&&x2>x&&y1<y&&y2>y);
    }
    
    public static void refresh(){
        
        Mwhell = Mouse.getDWheel();
        
        LMBdown = Mouse.isButtonDown(0);
        RMBdown = Mouse.isButtonDown(1);
        if(!LMBdown) LMBlock=false;
        if(!RMBdown) RMBlock=false;
        x = Mouse.getX();
        y = Mouse.getY();
        
        if(!Lgesture){
            if(LMBdown){
                Lgesture=!(LSgesture=false);
                lgsX=lgeX=x;
                lgsY=lgeY=y;
                lgdX=lgdY=0;
            }
        }else{
            lgeX=x;lgeY=y;            
            lgdX=lgeX-lgsX;
            lgdY=lgeY-lgsY;
            if(!LMBdown)
                Lgesture=!(LSgesture=true);
        }
        
        if(!Rgesture){
            if(RMBdown){
                Rgesture=!(RSgesture=false);
                rgsX=rgeX=x;
                rgsY=rgeY=y;
                rgdX=rgdY=0;
            }
        }else{
            rgeX=x;rgeY=y;            
            rgdX=rgeX-rgsX;
            rgdY=rgeY-rgsY;
            if(!RMBdown)
                Rgesture=!(RSgesture=true);
        }
    }    
}
